package test.crane;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.*;
import ptolemy.data.type.*;

//////////////////////////////////////////////////////////////////////////
//// posSensors
/**

@author Ivan Jeukens
@version v 0.1 07/02/2002
*/
public class posSensors extends TypedAtomicActor {

    public posSensors(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        input = new TypedIOPort(this, "input", true, false);
        input.setTypeEquals(BaseType.DOUBLE);

        SWPosCarMin = new TypedIOPort(this, "SWPosCarMin", false, true);
        SWPosCarMin.setTypeEquals(BaseType.BOOLEAN);
        
        SWPosCarMax = new TypedIOPort(this, "SWPosCarMax", false, true);
        SWPosCarMax.setTypeEquals(BaseType.BOOLEAN);
    }

    public TypedIOPort input;
    public TypedIOPort SWPosCarMin;
    public TypedIOPort SWPosCarMax;
    
    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void fire() throws IllegalActionException {
        if(input.hasToken(0)) {
            DoubleToken db = (DoubleToken) input.get(0);
            double pos = db.doubleValue();
            
            if(pos < -5.0) {
                SWPosCarMin.broadcast(new BooleanToken(true));
            }
            else {
                SWPosCarMin.broadcast(new BooleanToken(false));
            }
            
            if(pos > 5.0) {
                SWPosCarMax.broadcast(new BooleanToken(true));
            }
            else {
                SWPosCarMax.broadcast(new BooleanToken(false));
            }
        }
        
        if(input.isKnown() && !SWPosCarMax.isKnown()) {
            SWPosCarMax.broadcastClear();
            SWPosCarMin.broadcastClear();
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                        private methods                    ////

    ///////////////////////////////////////////////////////////////////
    ////                      private variables                    ////


}
